﻿using System;
using System.Collections.Generic;
 
using System.Text;
using System.IO;

namespace GGSharp.Packets
{
    public class MessagePacketBase : HeaderPacket
    {
        protected const byte FONT_BOLD_MASK = 0x01;
        protected const byte FONT_ITALIC_MASK = 0x02;
        protected const byte FONT_UNDERLINE_MASK = 0x04;
        protected const byte FONT_COLOR_MASK = 0x08;
        protected const byte FONT_IMAGE_MASK = 0x80;

        protected int _recipient;		/* numer odbiorcy */
        protected int _seq = 0;		/* numer sekwencyjny */
        protected int _class = 0x0004;		/* klasa wiadomości */
        protected string _message = "testowa";		/* treść */

        public int Number
        {
            get { return _recipient; }
        }

        public int Sequence
        {
            get { return _seq; }
        }

        public string Message
        {
            get { return _message; }
        }

        protected string ReadMessageText(BinaryReader reader)
        {
            byte[] buffer = new byte[2020];
            int i = 0;
            byte c = 0;
            do
            {
                c = reader.ReadByte();
                buffer[i++] = c;
            }
            while (c != 0);

            return ReadCP1250Text(buffer, 0, i - 1);
        }

        protected List<string> ReadRichtextFormat(BinaryReader reader)
        {
            short length = reader.ReadInt16();
            short readed = 0;

            List<string> opisy = new List<string>();

            while (readed < length)
            {
                short position = reader.ReadInt16();
                byte type = reader.ReadByte();

                readed += 3;

                string opis = "P(" + position + ")";

                if ((type & FONT_BOLD_MASK) == FONT_BOLD_MASK)
                    opis += "BOLD ";

                if ((type & FONT_ITALIC_MASK) == FONT_ITALIC_MASK)
                    opis += "ITALIC ";

                if ((type & FONT_UNDERLINE_MASK) == FONT_UNDERLINE_MASK)
                    opis += "UNDERLINE ";

                if ((type & FONT_COLOR_MASK) == FONT_COLOR_MASK)
                {
                    opis += "COLOR";
                    byte red = reader.ReadByte();
                    byte green = reader.ReadByte();
                    byte blue = reader.ReadByte();
                    opis += "(" + red + "," + green + "," + blue + ") ";

                    readed += 3;
                }

                if ((type & FONT_ITALIC_MASK) == FONT_IMAGE_MASK)
                {
                    opis += "IMAGE";
                    byte unknown = reader.ReadByte();
                    int size = reader.ReadInt32();
                    int crc32 = reader.ReadInt32();
                    opis += "(" + size + "b," + crc32 + ") ";

                    readed += 9;
                }
            }

            return opisy;
        }
    }
}
